<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label="事件编码" prop="sjCode">
        <el-input
          v-model="queryParams.sjCode"
          placeholder="请输入事件编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微震震源位置" prop="zyLocation">
        <el-input
          v-model="queryParams.zyLocation"
          placeholder="请输入微震震源位置"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发生时间">
        <el-date-picker
          v-model="daterangeOccurTime"
          size="small"
          style="width: 340px"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['realData:quakeRealData:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['realData:quakeRealData:edit']"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['realData:quakeRealData:remove']"
          >删除
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="quakeRealDataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="事件编码"
        align="center"
        prop="sjCode"
        width="180"
      />
      <el-table-column
        label="记录微震事件的通道号"
        align="center"
        prop="recordChannelNumber"
        width="180"
      />
      <el-table-column
        label="震源坐标X"
        align="center"
        prop="zyX"
        width="180"
      />
      <el-table-column
        label="震源坐标Y"
        align="center"
        prop="zyY"
        width="180"
      />
      <el-table-column
        label="震源坐标Z"
        align="center"
        prop="zyZ"
        width="180"
      />
      <el-table-column
        label="震源能量"
        align="center"
        prop="zyEnergy"
        width="180"
      />
      <el-table-column
        label="震源震级"
        align="center"
        prop="zyLevel"
        width="180"
      />
      <el-table-column
        label="微震震源位置"
        align="center"
        prop="zyLocation"
        width="180"
      />
      <el-table-column
        label="最大振幅"
        align="center"
        prop="zfMax"
        width="180"
      />
      <el-table-column
        label="平均振幅"
        align="center"
        prop="zfAvg"
        width="180"
      />
      <el-table-column
        label="微震事件波形主频"
        align="center"
        prop="wzEventZp"
        width="180"
      />
      <el-table-column
        label="发生时间"
        align="center"
        prop="occurTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.occurTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="事件分析结论"
        align="center"
        prop="eventAnalysisConclusion"
        width="180"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="140"
      >
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              size="small"
              type="success"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['realData:quakeRealData:edit']"
            >
            </el-button>
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['realData:quakeRealData:remove']"
            >
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改微震监测实时信息对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1100px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="事件编码" prop="sjCode">
              <el-input v-model="form.sjCode" placeholder="请输入事件编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="记录微震事件的通道号"
              prop="recordChannelNumber"
            >
              <el-input
                v-model="form.recordChannelNumber"
                placeholder="请输入记录微震事件的通道号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="震源坐标X" prop="zyX">
              <el-input v-model="form.zyX" placeholder="请输入震源坐标X" @input="inputnum"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="震源坐标Y" prop="zyY">
              <el-input v-model="form.zyY" placeholder="请输入震源坐标Y" @input="inputnum"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="震源坐标Z" prop="zyZ">
              <el-input v-model="form.zyZ" placeholder="请输入震源坐标Z" @input="inputnum"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="震源能量" prop="zyEnergy">
              <el-input v-model="form.zyEnergy" placeholder="请输入震源能量"  style="width:80%" @input="inputnum"/>
              <span class="unit">J</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="震源震级" prop="zyLevel">
              <el-input v-model="form.zyLevel" placeholder="请输入震源震级"  @input="inputnum"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微震震源位置" prop="zyLocation">
              <el-input
                v-model="form.zyLocation"
                placeholder="请输入微震震源位置"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最大振幅" prop="zfMax">
              <el-input v-model="form.zfMax" placeholder="请输入最大振幅" style="width:80%" @input="inputnum"/>
              <span class="unit">mV/mA</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="平均振幅" prop="zfAvg">
              <el-input v-model="form.zfAvg" placeholder="请输入平均振幅" style="width:80%" @input="inputnum"/>
              <span class="unit">mV/mA</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微震事件波形主频" prop="wzEventZp" >
              <el-input
                v-model="form.wzEventZp"
                placeholder="请输入微震事件波形主频"
                style="width: 80%;" @input="inputnum"
              />
              <span class="unit">Hz</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发生时间" prop="occurTime">
              <el-date-picker
                clearable
                size="small"
                v-model="form.occurTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择时间"
                style="width: 100%;"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="事件分析结论" prop="eventAnalysisConclusion">
              <el-input
                v-model="form.eventAnalysisConclusion"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listQuakeRealData,
  getQuakeRealData,
  delQuakeRealData,
  addQuakeRealData,
  updateQuakeRealData
} from "@/api/realData/quakeRealData";

export default {
  name: "QuakeRealData",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 微震监测实时信息表格数据
      quakeRealDataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 发生时间时间范围
      daterangeOccurTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mineCode: null,

        mineName: null,

        cdCode: null,

        sjCode: null,

        recordChannelNumber: null,

        zyX: null,

        zyY: null,

        zyZ: null,

        zyEnergy: null,

        zyLevel: null,

        zyLocation: null,

        zfMax: null,

        zfAvg: null,

        wzEventZp: null,

        occurTime: null,

        eventAnalysisConclusion: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sjCode: [
          { required: true, message: "事件编码不能为空", trigger: "blur" }
        ],
        zyX: [
          { required: true, message: "震源坐标X不能为空", trigger: "blur" },
          { min: 1, max: 10, message: "长度在 1 到 10 个字符", trigger: "blur" }
        ],
        zyY: [
          { required: true, message: "震源坐标Y不能为空", trigger: "blur" },
          { min: 1, max: 10, message: "长度在 1 到 10 个字符", trigger: "blur" }
        ],
        zyZ: [
          { required: true, message: "震源坐标Z不能为空", trigger: "blur" },
          { min: 1, max: 10, message: "长度在 1 到 10 个字符", trigger: "blur" }
        ],
        zyEnergy: [
          { required: true, message: "震源能量不能为空", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],
        zyLevel: [
          { required: true, message: "震源震级不能为空", trigger: "blur" },
          { min: 1, max: 4, message: "长度在 1 到 4 个字符", trigger: "blur" }
        ],
        zyLocation: [
          { required: true, message: "微震震源位置不能为空", trigger: "blur" }
        ],
        zfMax: [
          { required: true, message: "最大振幅不能为空", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],
        zfAvg: [
          { required: true, message: "平均振幅不能为空", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],
        wzEventZp: [
          {
            required: true,
            message: "微震事件波形主频不能为空",
            trigger: "blur"
          },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],
        occurTime: [
          { required: true, message: "发生时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 限制只能在输入框中输入小数点后两位
    inputnum() {
      this.form.zyX = this.form.zyX.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
      this.form.zyY = this.form.zyY.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
      this.form.zyZ = this.form.zyZ.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
      this.form.zyEnergy = this.form.zyEnergy.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
      this.form.zyLevel = this.form.zyLevel.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
      this.form.zfMax = this.form.zfMax.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
      this.form.zfAvg = this.form.zfAvg.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
      this.form.wzEventZp = this.form.wzEventZp.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
    },
    /** 查询微震监测实时信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeOccurTime && "" != this.daterangeOccurTime) {
        this.queryParams.params["beginOccurTime"] = this.daterangeOccurTime[0];
        this.queryParams.params["endOccurTime"] = this.daterangeOccurTime[1];
      }
      listQuakeRealData(this.queryParams).then(response => {
        this.quakeRealDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        mineCode: null,

        mineName: null,

        cdCode: null,

        sjCode: null,

        recordChannelNumber: null,

        zyX: null,

        zyY: null,

        zyZ: null,

        zyEnergy: null,

        zyLevel: null,

        zyLocation: null,

        zfMax: null,

        zfAvg: null,

        wzEventZp: null,

        occurTime: null,

        eventAnalysisConclusion: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeOccurTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加微震监测实时信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getQuakeRealData(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改微震监测实时信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateQuakeRealData(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQuakeRealData(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除当前选中数据？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delQuakeRealData(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    }
  }
};
</script>
